package com.byx.chatclient.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.byx.chatclient.R;
import com.nurmemet.nur.nurvideoplayer.NurVideoView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class VideoPlayerActivity extends AppCompatActivity {
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.video_view)
    protected NurVideoView nurVideoView;
    public static final String INTENT_VIDEO_PATH = "VIDEO_PATH";
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_video_player);
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        String uri = intent.getStringExtra(INTENT_VIDEO_PATH);
        assert uri != null;
        String name = uri.substring(uri.lastIndexOf('/') + 1);
        nurVideoView.setUp(this,uri,name);

    }

    @Override
    public void onBackPressed() {
        if (nurVideoView.getIsFullScreen()) {
            nurVideoView.setChangeScreen(false);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        nurVideoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        nurVideoView.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nurVideoView.stopPlay();
        unbinder.unbind();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean b = nurVideoView.onKeyDown(keyCode);
        return b || super.onKeyDown(keyCode, event);
    }
}